package com.juzi.wopu;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;

public class BaseDB {
	 public Connection getConnection(){
         String pm_password=null;
         String pm_user_name=null;
         String pm_jdbc_url=null;
         String pm_driver_name=null;

  Properties propers = new Properties();
       try{
           propers.load(this.getClass().getClassLoader().getResourceAsStream("jdbc.properties"));
           pm_password=propers.getProperty("jdbc.password");
           pm_user_name=propers.getProperty("jdbc.username");
           pm_jdbc_url=propers.getProperty("jdbc.url");
           pm_driver_name=propers.getProperty("jdbc.driverClassName");
            Class.forName(pm_driver_name);
         return (Connection) DriverManager.getConnection(pm_jdbc_url,pm_user_name,pm_password);
           }catch(Exception ex){
                    ex.printStackTrace();
                 }
         return null;
     }	
	 
     public static void main(String[] args){
    	 BaseDB db=new BaseDB();
         Connection con=db.getConnection();
         System.out.println(con);  
     }
     public int ExceSql(Connection con,String sSql)
 	{
 		try {
 			Statement stmt = con.createStatement();
 			return stmt.executeUpdate(sSql);	
 		} catch (SQLException e) {
 			// TODO Auto-generated catch block
 			//e.printStackTrace();
 			LoggerUtil.log(sSql, Level.WARNING);
 			LoggerUtil.log(e.getMessage(), Level.WARNING);
 			return -1;
 		} 
 	}
 	
 	public static ResultSet OpenSql(Connection con,String sSql)
 	{
 		try {
 			Statement stmt = con.createStatement();
 			return stmt.executeQuery(sSql);	
 		} catch (SQLException e) {
 			// TODO Auto-generated catch block
 			//e.printStackTrace();
 			LoggerUtil.log(sSql, Level.WARNING);
 			LoggerUtil.log(e.getMessage(), Level.WARNING);
 			return null;
 		} 
 	}
 	
 	//执行多条语句
 	public static ResultSet ExceAndOpenSql(Connection con,String sSqls)
 	{
 		try {
 			Statement stmt = con.createStatement();
 			stmt.getMoreResults();
 			stmt.execute(sSqls);	
 			stmt.getMoreResults();
 			return stmt.getResultSet(); 
 		} catch (SQLException e) {
 			// TODO Auto-generated catch block
 			//e.printStackTrace();
 			LoggerUtil.log(sSqls, Level.WARNING);
 			LoggerUtil.log(e.getMessage(), Level.WARNING);
 			return null;
 		} 
 	}
 	
 	//执行多条语句
 		public static boolean ExceAnySql(Connection con,String sSqls)
 		{
 			try {
 				Statement stmt = con.createStatement();
 				stmt.execute(sSqls);	
 				return true;
 			} catch (SQLException e) {
 				// TODO Auto-generated catch block
 				//e.printStackTrace();
 				LoggerUtil.log(sSqls, Level.WARNING);
 				LoggerUtil.log(e.getMessage(), Level.WARNING);
 				return false;
 			} 
 		}
 	
}
